//
//  ZMYTabBar.m
//  XMJJ_zml
//
//  Created by 杨何华 on 2025/5/9.
//

#import "ZMYTabBar.h"
#import <UIKit/UIKey.h>
@interface ZMYTabBar ()

@property (nonatomic, strong) NSMutableArray<UIButton *> *tabButtons;
@property (nonatomic, strong) UIView *underlineView;
@property (nonatomic, strong) NSMutableArray *buttonWidths;

@end

@implementation ZMYTabBar
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray<NSString *> *)titles {
    self = [super initWithFrame:frame];
    if (self) {
        self.tabTitles = titles;
        self.selectedIndex = 0;
        self.normalTextColor = kColorHEX(0xCBC3D2);
        self.selectedTextColor = kColorHEX(333333);
        self.underlineColor = kColorHEX(0xFFD800);
        self.underlineHeight = 2.0;
        self.normalFont = [UIFont systemFontOfSize:15];
        self.selectedFont = [UIFont boldSystemFontOfSize:15];
        
        [self setupSubviews];
    }
    return self;
}


- (void)setupSubviews {
    self.tabButtons = [NSMutableArray array];
    self.buttonWidths = [NSMutableArray array];
    
    // 计算每个标签的宽度（根据文本内容自适应）
    CGFloat totalWidth = 0;
    for (NSString *title in self.tabTitles) {
        CGSize size = [title sizeWithAttributes:@{NSFontAttributeName: self.normalFont}];
        CGFloat width = size.width + 20; // 添加一些边距
        [self.buttonWidths addObject:@(width)];
        totalWidth += width;
    }
    
    // 计算标签之间的间距
    CGFloat spacing = (self.bounds.size.width - totalWidth) / (self.tabTitles.count + 1);
    
    // 创建标签按钮
    CGFloat currentX = spacing;
    for (NSInteger i = 0; i < self.tabTitles.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat width = [self.buttonWidths[i] floatValue];
        button.frame = CGRectMake(currentX, 0, width, self.bounds.size.height - self.underlineHeight);
        [button setTitle:self.tabTitles[i] forState:UIControlStateNormal];
//        [button setTitleColor:self.selectedTextColor forState:UIControlStateSelected];
        if(i==0){
            [button setTitleColor:self.selectedTextColor forState:UIControlStateNormal];
//            [button setTitleColor:self.selectedTextColor forState:UIControlStateNormal];
        }else {
            [button setTitleColor:self.normalTextColor forState:UIControlStateNormal];
         }
        button.titleLabel.font = self.normalFont;
        button.tag = i;
        [button addTarget:self action:@selector(tabButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        [self.tabButtons addObject:button];
        
        currentX += width + spacing;
    }
    
    // 设置初始选中状态
    [self selectTabAtIndex:self.selectedIndex animated:NO];
   
    // 创建下划线视图
    self.underlineView = [[UIView alloc] initWithFrame:CGRectMake(spacing + 12, self.bounds.size.height - self.underlineHeight - 5, [self.buttonWidths[self.selectedIndex] floatValue] - 15-10, self.underlineHeight)];
    self.underlineView.backgroundColor = self.underlineColor;
    self.underlineView.layer.cornerRadius = self.underlineHeight / 2;
    self.underlineView.layer.masksToBounds = YES;
    [self addSubview:self.underlineView];
}

- (void)tabButtonTapped:(UIButton *)button {
//    if(button.tag!=0){
//        [button setTitleColor:self.normalTextColor forState:UIControlStateNormal];
//     }
//    [button setTitleColor:self.normalTextColor forState:UIControlStateNormal];
//    [button setTitleColor:self.selectedTextColor forState:UIControlStateSelected];
    [self selectTabAtIndex:button.tag animated:YES];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(ZMYTabBar:didSelectTabAtIndex:)]) {
        [self.delegate ZMYTabBar:self didSelectTabAtIndex:button.tag];
    }
}

- (void)selectTabAtIndex:(NSInteger)index animated:(BOOL)animated {
    if (index < 0 || index >= self.tabButtons.count || index == self.selectedIndex) {
        return;
    }
    
    // 取消之前选中的按钮
    UIButton *prevButton = self.tabButtons[self.selectedIndex];
    prevButton.selected = NO;
   [prevButton setTitleColor:self.normalTextColor forState:UIControlStateNormal];

    prevButton.titleLabel.font = self.normalFont;
    
    // 设置新选中的按钮
    self.selectedIndex = index;
    UIButton *selectedButton = self.tabButtons[self.selectedIndex];
    selectedButton.selected = YES;
    [selectedButton setTitleColor:self.selectedTextColor forState:UIControlStateNormal];
    selectedButton.titleLabel.font = self.selectedFont;
    
    // 更新下划线位置和宽度
    CGRect newFrame = CGRectMake(selectedButton.frame.origin.x + 12,
                                 self.bounds.size.height - self.underlineHeight - 5,
                                 selectedButton.frame.size.width - 15-10,
                                 self.underlineHeight);
    
//    if (animated) {
//        [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{
            self.underlineView.frame = newFrame;
//        } completion:nil];
//    } else {
//        self.underlineView.frame = newFrame;
//    }
}


@end
